//
//  KunRecordDetailVC.swift
//  KunLanCamera
//
//  Created by 李东 on 2024/3/24.
//

import UIKit

class KunRecordDetailVC: KunBaseVC {
    @IBOutlet weak var titleBgView_top: NSLayoutConstraint!
    
    @IBOutlet weak var titleBgView: UIView!
    @IBOutlet weak var titleTF: UITextField!
    @IBOutlet weak var weatherImgView: UIImageView!
    @IBOutlet weak var weatherBgBiew: UIView!
    @IBOutlet weak var contentPlaceholder: UILabel!
    @IBOutlet weak var contentTV: UITextView!
    var saveBtn: UIButton!
    var model = KunRecordModel()
    
    init(model: KunRecordModel = KunRecordModel()) {
        super.init(nibName: nil, bundle: nil)
        self.model = model
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showBgImgView = true
        showPopBtn = true
        titleBgView_top.constant = CGFloat.navHeight + 20
        saveBtn = UIButton()
        saveBtn.setImage(UIImage(named: "icon_edit_finish"), for: .normal)
        saveBtn.addTarget(self, action: #selector(saveHandle), for: .touchUpInside)
        view.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.centerY.equalTo(popBtn)
            make.size.equalTo(popBtn)
            make.right.equalTo(-15)
        }
      
        
        contentTV.delegate = self
        
        weatherBgBiew.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectWeatherHandle)))
        
        if model.ts != 0 {
            titleTF.text = model.title
            weatherImgView.image = UIImage(named: model.weather)
            contentTV.text = model.content
            contentPlaceholder.isHidden = !contentTV.text.isEmpty
        }else {
            model.weather = "晴"
        }
    }
    
    lazy var df: DateFormatter = {
        let tt = DateFormatter()
        tt.dateFormat = "yyyy.MM.dd HH:mm EE"
        tt.locale = Locale(identifier: "zh_CN")
        return tt
    }()
    
    @objc func saveHandle() {
        if titleTF.text?.isEmpty ?? true {
            showHUDTextOnly("请输入标题")
            return
        }
        if contentTV.text.isEmpty {
            showHUDTextOnly("请输入正文")
            return
        }
        model.content = contentTV.text
        model.title = titleTF.text ?? ""
        if model.ts == 0 {
            model.ts = Int(Date().timeIntervalSince1970)
            let str = df.string(from: Date())
            let list = str.components(separatedBy: " ")
            model.date = list[0]
            model.time = list[1]
            model.week = list[2]
        }
        KunDataHelper.shared.addData(model: model)
        showHUDTextOnly("当前美篇内容已保存") {[weak self] in
            guard let self = self else {
                return
            }
            self.navigationController?.popViewController(animated: true)
        }
        
    }
    
    @objc func selectWeatherHandle() {
        let vc = KunWeatherPickerVC()
        vc.modalPresentationStyle = .overCurrentContext
        vc.selectCallBack = {[weak self] ww in
            guard let self = self else {
                return
            }
            self.model.weather = ww
            self.weatherImgView.image = UIImage(named: ww)
        }
        present(vc, animated: true)
    }



}

extension KunRecordDetailVC: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        contentPlaceholder.isHidden = !textView.text.isEmpty
    }
}
